package com.zeroone.star.publish.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zeroone.star.project.dto.PageDTO;
import com.zeroone.star.project.vo.JsonVO;
import com.zeroone.star.publish.entity.User;
import com.zeroone.star.publish.entity.Volunteer;
import com.zeroone.star.publish.service.impl.VolunteerServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author car
 * @since 2024-03-02
 */
@RestController
@RequestMapping("/publish/volunteer")
public class VolunteerController {

    @Resource
    private VolunteerServiceImpl volunteerService;

    /**
     * @return com.zeroone.star.project.vo.JsonVO
     * @Author car
     * @Description //TODO 新增志愿
     * @Date 11:12 2024/3/2
     * @Param [com.zeroone.star.publish.entity.Volunteer]
     **/
    @PostMapping("/addVolunteer")
    @ApiOperation("新增志愿信息")
    public JsonVO addVolunteer(@RequestBody Volunteer volunteer) {
        JsonVO jsonVO = new JsonVO();
        if (volunteerService.addVolunteer(volunteer) == 1) {
            jsonVO.setMessage("新增成功");
            jsonVO.setCode(200);
        } else {
            jsonVO.setMessage("新增失败");
            jsonVO.setCode(201);
        }
        return jsonVO;
    }


    /**
     * @return com.zeroone.star.project.vo.JsonVO<com.zeroone.star.publish.entity.Volunteer>
     * @Author car
     * @Description //TODO 更改志愿信息
     * @Date 11:14 2024/3/2
     * @Param [com.zeroone.star.publish.entity.Volunteer]
     **/
    @PostMapping("/modifyVolunteer")
    @ApiOperation("修改志愿信息")
    public JsonVO<Volunteer> modifyVolunteer(Volunteer volunteer) {
        Volunteer res = volunteerService.modifyVolunteer(volunteer);
        JsonVO<Volunteer> vo = new JsonVO<>();
        if (res == null) {
            vo.setCode(201);
            vo.setMessage("修改失败");
        } else {
            vo.setData(volunteer);
            vo.setMessage("修改成功");
        }

        return vo;
    }


    /**
     * @return springfox.documentation.spring.web.json.Json
     * @Author car
     * @Description //TODO 删除志愿信息
     * @Date 11:15 2024/3/2
     * @Param [com.zeroone.star.publish.entity.Volunteer]
     **/
    @PostMapping("/deleteVolunteerById")
    @ApiOperation("根据id删除volunteer")
    public JsonVO deleteVolunteerById(Integer id) {
        JsonVO jsonVO = new JsonVO();
        if (volunteerService.deleteVolunteer(id) == 1) {
            jsonVO.setCode(200);
            jsonVO.setMessage("删除成功");
        } else {
            jsonVO.setCode(201);
            jsonVO.setMessage("删除失败");
        }
        return jsonVO;
    }


    @GetMapping("/selectVolunteerById")
    @ApiOperation("根据id进行查看详情")
    public JsonVO<Volunteer> selectVolunteerById(@RequestParam Integer id) {
        Volunteer volunteer = volunteerService.selectVolunteerById(id);
//        新建了一个Volunter的对象：volunteer，用这个对象存储服务器上查出来的志愿者id
        JsonVO<Volunteer> jsonVO = new JsonVO<>();
        if (volunteer.getId() == null) {
            jsonVO.setMessage("查询失败");
            jsonVO.setData(volunteer);
//            如果查询失败则返回201，打印查询失败
            jsonVO.setCode(201);

        } else {
            jsonVO.setCode(200);
            jsonVO.setMessage("查询成功");
            jsonVO.setData(volunteer);
//            查询成功就返回200，打印查询成功
        }
        return jsonVO;
    }

    @GetMapping("/selectAllVolunteer")
    @ApiOperation("查询全部志愿信息")
    public JsonVO<List<Volunteer>> selectAllVolunteer() {
//        String authorization = httpServletRequest.getHeader("Authorization").replace("Bearer ", "");
        List<Volunteer> volunteers = volunteerService.selectAllVolunteer();
        JsonVO<List<Volunteer>> listJsonVO = new JsonVO<>();
        listJsonVO.setData(volunteers);
        listJsonVO.setCode(200);
        listJsonVO.setMessage("查询成功");
        return listJsonVO;
    }


    @GetMapping("/selectOwnAllVolunteer")
    @ApiOperation("查询某人的志愿信息")
    public JsonVO<List<Volunteer>> selectOwnAllVolunteer(@RequestParam("uid") String uid){
        List<Volunteer> volunteers = volunteerService.selectOwnAllVolunteer(uid);
        JsonVO<List<Volunteer>> listJsonVO = new JsonVO<>();
        listJsonVO.setData(volunteers);
        listJsonVO.setCode(200);
        listJsonVO.setMessage("查询成功");
        return listJsonVO;
    }

    @GetMapping("/selectAll")
    @ApiOperation("查询所有志愿者")
    public List<Volunteer> selectAll(){
        return volunteerService.list();
    }



    @GetMapping("/selectLikeByPage")
    @ApiOperation("通过志愿内容模糊查询志愿信息")
    public PageDTO<Volunteer> selectLikeByPage(Integer currentIndex,String content){
        IPage<Volunteer> volunteerIPage = volunteerService.selectByPageLikeContent(content, currentIndex);
        List<Volunteer> records = volunteerIPage.getRecords();
        PageDTO<Volunteer> pageDTO = new PageDTO<>();
        pageDTO.setPageIndex(Long.valueOf(currentIndex));
        pageDTO.setPageSize(10L);
        pageDTO.setRows(records);
        return pageDTO;
    }

}

